package com.sup.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sup.entity.Category;
import com.sup.entity.Project;
import com.sup.entity.User;

/**
 * Servlet implementation class AddProjectServlet
 */
@WebServlet("/auth/addProject")
public class AddProjectServlet extends HttpServlet {
	private static EntityManagerFactory emf;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddProjectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public void init()
    {
    	emf = Persistence.createEntityManagerFactory("PersistenceUnit");
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		EntityManager em = emf.createEntityManager();
		
		Query listCategory = em.createQuery("SELECT Category from Category category");
		List<Category> categories = listCategory.getResultList();
		request.setAttribute("categories", categories);
		
		request.getRequestDispatcher("/auth/addProject.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		EntityManager em = emf.createEntityManager();
		
		String idUser = (String) request.getSession().getAttribute("idUser");
		Query User = em.createQuery("SELECT User from User user WHERE idUser="+idUser);
		User user = (User) User.getSingleResult();
		
		String nameCategory = (String) request.getParameter("category");
		Query Category = em.createQuery("SELECT Category FROM Category category WHERE name='"+nameCategory+"'");
		Category category = (Category)Category.getSingleResult();
		
		Project newProject = new Project();
		newProject.setName((String)request.getParameter("name"));
		newProject.setDescription((String)request.getParameter("description"));
		newProject.setDateCreation(new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()));
		newProject.setDateFin((String)request.getParameter("dateFin"));
		newProject.setUser(user);
		newProject.setCategory(category);
		
		em.getTransaction().begin();
	    em.persist(newProject);
	    em.refresh(newProject);
	    em.getTransaction().commit();
	    
	    String idNewProject = newProject.getIdProject();
	    em.close();
	    
	    response.sendRedirect(getServletContext().getContextPath()+"/showProject?id="+idNewProject);
	}

}
